package com.yaozheng.vocationaltraining.dialog.service;

import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.dialog.BaseDialog;
import com.yaozheng.vocationaltraining.service.BaseService;
import com.yaozheng.vocationaltraining.view.dialog.ConfirmCancelAskDialogView;
import com.yaozheng.vocationaltraining.view.dialog.ConfirmCancelAskDialogView_;

/**
 */
public class ConfirmCancelAskDialog extends BaseDialog {
    ConfirmCancelAskDialogView confirmCancelAskDialogView;
    String runMethod;
    private BaseService baseService;

    public ConfirmCancelAskDialog(BaseService baseService) {
        super(baseService, R.style.baseDialogStyleTheme, true, Gravity.CENTER);
        this.baseService = baseService;
    }


    public void setRunMethod(String runMethod) {
        this.runMethod = runMethod;
    }

    @Override
    public void initBaseDialog() {
        Window window = getWindow();
        WindowManager.LayoutParams layoutpara = window
                .getAttributes();
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        layoutpara.gravity = gravity;
        if (isCover) {
            layoutpara.dimAmount = (float) (getContext().getResources().getInteger(R.integer.baseDialogdimAmount) / 10.0); // 添加背景遮盖
        } else {
            layoutpara.dimAmount = 0f;
        }
        layoutpara.width = WindowManager.LayoutParams.FILL_PARENT;
        setAttributes(window, layoutpara);
        OnKeyListener keylistener = new OnKeyListener() {
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    return true;
                } else {
                    return false;
                }
            }
        };
        setOnKeyListener(keylistener);
    }

    @Override
    public void show() {
        super.show();
        setCanceledOnTouchOutside(false);
    }

    @Override
    public void setContentView() {
        confirmCancelAskDialogView = ConfirmCancelAskDialogView_.build(getContext());
        confirmCancelAskDialogView.setBaseDialogEventProcess(this);
        setContentView(confirmCancelAskDialogView);
    }

    /**
     * 设置标题并且内容
     *
     * @param title
     * @param content
     */
    public void setTitleAndroidContent(String title, String content) {
        confirmCancelAskDialogView.setTitleAndroidContent(title, content);
    }


    /**
     * 设置内容
     *
     * @param content
     */
    public void setContent(String content) {
        setContent(content, "请选择");
    }

    /**
     * 设置内容
     *
     * @param content
     */
    public void setContent(String content, String title) {
        confirmCancelAskDialogView.setTitleAndroidContent(title, content);
    }

    /**
     * 确定
     */
    public void baseCancel() {
        cancel();
        if (baseService != null) {
            baseService.runMethod("cancelClick");
        }

    }


    /**
     * 确定
     */
    public void determine() {
        if (runMethod == null) {
            runMethod = "determineClick";
        }
        cancel();
        if (baseService != null) {
            baseService.runMethod(runMethod);
        }

    }
}
